{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 61. Rotate List\n",
    "\n",
    "Given a linked list, rotate the list to the right by k places, where k is non-negative.\n",
    "\n",
    "    Example 1:\n",
    "        Input: 1->2->3->4->5->NULL, k = 2\n",
    "        Output: 4->5->1->2->3->NULL\n",
    "        Explanation:\n",
    "        rotate 1 steps to the right: 5->1->2->3->4->NULL\n",
    "        rotate 2 steps to the right: 4->5->1->2->3->NULL\n",
    "\n",
    "    Example 2:\n",
    "        Input: 0->1->2->NULL, k = 4\n",
    "        Output: 2->0->1->NULL\n",
    "        Explanation:\n",
    "        rotate 1 steps to the right: 2->0->1->NULL\n",
    "        rotate 2 steps to the right: 1->2->0->NULL\n",
    "        rotate 3 steps to the right: 0->1->2->NULL\n",
    "        rotate 4 steps to the right: 2->0->1->NULL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# Definition for singly-linked list.\n",
    "class ListNode:\n",
    "    def __init__(self, x):\n",
    "        self.val = x\n",
    "        self.next = None\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def rotateRight(self, head: ListNode, k: int) -> ListNode:\n",
    "        # base cases\n",
    "        if not head or head.next is None:\n",
    "            return head\n",
    "\n",
    "        # close the linked list into the ring\n",
    "        old_tail = head\n",
    "        n = 1\n",
    "        while old_tail.next:\n",
    "            old_tail = old_tail.next\n",
    "            n += 1\n",
    "        old_tail.next = head\n",
    "\n",
    "        # find new tail : (n - k % n - 1)th node\n",
    "        # and new head : (n - k % n)th node\n",
    "        new_tail = head\n",
    "        for i in range(n - k % n -1):\n",
    "            new_tail = new_tail.next\n",
    "        new_head = new_tail.next\n",
    "\n",
    "        # break the ring\n",
    "        new_tail.next = None\n",
    "\n",
    "        return new_head\n",
    "\n",
    "\n",
    "S = Solution()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 5, 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "x = [1, 2, 3, 4, 5]\n",
    "k = 2\n",
    "head = ListNode(x[0])\n",
    "temp = head\n",
    "for i in range(1, len(x)):\n",
    "    temp.next = ListNode(x[i])\n",
    "    temp = temp.next\n",
    "output = S.rotateRight(head, k)\n",
    "ans = [output.val]\n",
    "while output.next:\n",
    "    ans.append(output.next.val)\n",
    "    output = output.next\n",
    "print(ans)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 0, 1]\n"
     ]
    }
   ],
   "source": [
    "x = [0, 1, 2]\n",
    "k = 4\n",
    "head = ListNode(x[0])\n",
    "temp = head\n",
    "for i in range(1, len(x)):\n",
    "    temp.next = ListNode(x[i])\n",
    "    temp = temp.next\n",
    "output = S.rotateRight(head, k)\n",
    "ans = [output.val]\n",
    "while output.next:\n",
    "    ans.append(output.next.val)\n",
    "    output = output.next\n",
    "print(ans)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "x = [1]\n",
    "k = 1\n",
    "head = ListNode(x[0])\n",
    "output = S.rotateRight(head, k)\n",
    "ans = [output.val]\n",
    "while output.next:\n",
    "    ans.append(output.next.val)\n",
    "    output = output.next\n",
    "print(ans)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}